#include<string.h>
#include<malloc.h>

#include"user.h"

int is_name_equal(void* o1, void* o2){
    char* name1=(char*)o1;
    char* name2=(char*)o2;
    return strncmp(name1,name2,USER_NAME_LEN);
}

void free_name_ptr(void *data){
    free((char*)data);
}

User* create_user(const char *name){
    User* this=(User*)calloc(1,sizeof(User));
    strncpy(this->name,name,USER_NAME_LEN);
    this->name[USER_NAME_LEN]='\0';
    this->friends=create_list();
    return this;
}

int find_friend(User* this, char* friend_name){
    if(this==NULL || friend_name==NULL)return -1;
    int index=find_node(this->friends,friend_name,is_name_equal);
    return index;
}

int make_friend(User* this,char* friend_name){
    int index=find_friend(this,friend_name);
    if(index!=-1)return -1;

    add_node(this->friends,friend_name);
    return 0;
}

User* destory_user(User *this){
    if(this==NULL)return NULL;
    iter_list(this->friends,free_name_ptr);
    clear_list(this->friends);
    free(this);
    return NULL;
}
